package com.gyz.CA;

import java.util.Arrays;
import java.util.Random;

public class CA {
    static int size = 400;

    static double P = 1;

    static int state = 180;

    public static void main(String[] args) {
        int cas = 2000;
        Random random = new Random();

        Cell[][] cells = new Cell[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                cells[i][j] = new Cell();
            }
        }
/*        for (int i = 0; i < 400; i++) {
            for (int j = 0; j < 400; j++) {
                cells[i][j] = -1;
            }
        }
        for (int i = 0; i < 200; i++) {
            for (int j = 0; j < 400; j++) {
                cells[i][j] = -362145;
            }
        }*/

        //System.out.println(cells[0][0]);

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                //cells[i][j].setColor(random.nextBoolean());
                cells[i][j].setBebig(random.nextDouble());
                cells[i][j].setState(random.nextInt(state));
            }
        }

        //判断晶界
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if(cells[i][j].getState() != cells[i][(j+1)%size].getState() || cells[i][j].getState() != cells[(i+1)%size][j].getState()){
                    cells[i][j].setColor(false);
                }else{
                    cells[i][j].setColor(true);
                }
            }
        }

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (cells[i][j].getColor()){
                    cells[i][j].setGb(1);
                }else {
                    cells[i][j].setGb(0);
                }
            }
        }

        toBig(cells,cas);

/*        int cas = 2000;
        while (cas>0){
            cas--;
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    if(cells[i][j].getBebig()> P && cells[i][j].getGb() == 0){
                        int nums = 0;
                        if (cells[i][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[i][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][j].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][j].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (nums >=5){
                            cells[i][j].setGb(1);
                            cells[i][j].setColor(true);
                            continue;
                        }
                        nums = 0;
                        if (cells[i][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[i][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][j].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][j].getGb() == 1)
                            nums++;
                        if (nums >=3){
                            cells[i][j].setGb(1);
                            cells[i][j].setColor(true);
                            continue;
                        }

                        nums = 0;
                        if (cells[(i-1+size)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (nums >=3){
                            cells[i][j].setGb(1);
                            cells[i][j].setColor(true);
                            continue;
                        }
                    }
                }
            }
        }*/


        /*int[][] imageArray = new int[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if(cells[i][j].getColor()){
                    imageArray[i][j] = -1;
                }else {
                    imageArray[i][j] = -362145;
                }

            }
        }
        ImageWithArray.writeImageFromArray("F:\\test\\cas"+"P" + P +cas+"state"+state+".png", "PNG", imageArray);*/
    }

    public static void toBig(Cell[][] cells, int cas){
        //int cas = 2000;
        while (cas>0){
            int temp = 0;
            cas--;
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    //是晶界才变化
                    if(cells[i][j].getGb() == 1)
                        continue;

                    if(cells[i][j].getBebig() < P){
                        int nums = 0;
                        int[] stateNule1 = {cells[i][(j-1+size)%size].getState(),cells[i][(j+1)%size].getState(),cells[(i+1)%size][j].getState(),
                                cells[(i-1+size)%size][j].getState(),cells[(i-1+size)%size][(j-1+size)%size].getState(),
                                cells[(i-1+size)%size][(j+1)%size].getState(),
                                cells[(i+1)%size][(j+1)%size].getState(),cells[(i+1)%size][(j-1+size)%size].getState()};

                        Arrays.sort(stateNule1);
                        if(stateNule1[0] == stateNule1[4]){
                            cells[i][j].setState(stateNule1[0]);
                            continue;
                        }else if(stateNule1[1] == stateNule1[5]){
                            cells[i][j].setState(stateNule1[1]);
                            continue;
                        }else if(stateNule1[2] == stateNule1[6]){
                            cells[i][j].setState(stateNule1[2]);
                            continue;
                        }else if(stateNule1[3] == stateNule1[7]){
                            cells[i][j].setState(stateNule1[3]);
                            continue;
                        }

                        int[] stateNule2 = {cells[i][(j-1+size)%size].getState(),cells[i][(j+1)%size].getState(),cells[(i+1)%size][j].getState(),
                                cells[(i-1+size)%size][j].getState()};

                        Arrays.sort(stateNule2);
                        if(stateNule2[0] == stateNule2[2]){
                            cells[i][j].setState(stateNule2[0]);
                            continue;
                        }else if(stateNule2[1] == stateNule2[3]){
                            cells[i][j].setState(stateNule2[1]);
                            continue;
                        }

                        int[] stateNule3 = {cells[(i-1+size)%size][(j-1+size)%size].getState(),
                                cells[(i-1+size)%size][(j+1)%size].getState(),
                                cells[(i+1)%size][(j+1)%size].getState(),cells[(i+1)%size][(j-1+size)%size].getState()};

                        Arrays.sort(stateNule3);
                        if(stateNule3[0] == stateNule3[2]){
                            cells[i][j].setState(stateNule3[0]);
                            continue;
                        }else if(stateNule3[1] == stateNule3[3]){
                            cells[i][j].setState(stateNule3[0]);
                            continue;
                        }


                        Random rd = new Random();
                        int r1 = rd.nextInt(3)-1;
                        int r2 = rd.nextInt(3)-1;
                        int Ei = 0;
                        int Ej = 0;
                        if(cells[i][j].getState() != cells[i][(j-1+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[i][(j+1+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[(i+1+size)%size][(j+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[(i-1+size)%size][(j+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[(i+1+size)%size][(j-1+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[(i-1+size)%size][(j+1+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[(i-1+size)%size][(j-1+size)%size].getState())
                            Ei++;
                        if(cells[i][j].getState() != cells[(i+1+size)%size][(j+1+size)%size].getState())
                            Ei++;
                        //-----------------------------Ej--------------------------------------
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[(i+r1+size)%size][((j+r2+size)%size-1+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[(i+r1+size)%size][((j+r2+size)%size+1+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[((i+r1+size)%size+1+size)%size][((j+r2+size)%size+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[((i+r1+size)%size-1+size)%size][((j+r2+size)%size+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[((i+r1+size)%size+1+size)%size][((j+r2+size)%size-1+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[((i+r1+size)%size-1+size)%size][((j+r2+size)%size+1+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[((i+r1+size)%size-1+size)%size][((j+r2+size)%size-1+size)%size].getState())
                            Ej++;
                        if(cells[(i+r1+size)%size][(j+r2+size)%size].getState() != cells[((i+r1+size)%size+1+size)%size][((j+r2+size)%size+1+size)%size].getState())
                            Ej++;
                            /*cells[(i+1)%size][j].getState(),
                                cells[(i-1+size)%size][j].getState(),cells[(i-1+size)%size][(j-1+size)%size].getState(),
                                cells[(i-1+size)%size][(j+1)%size].getState(),
                                cells[(i+1)%size][(j+1)%size].getState(),cells[(i+1)%size][(j-1+size)%size].getState()*/
                        int diffE = Ej-Ei;
                        if(diffE <=0){
                            cells[i][j].setState(cells[(i+r1+size)%size][(j+r2+size)%size].getState());
                        }

/*                        if (cells[i][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[i][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][j].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][j].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j-1+size)%size].getGb() == 1)
                            nums++;*/
/*                        if (nums >=5){
                            cells[i][j].setGb(1);
                            cells[i][j].setColor(true);
                            continue;
                        }
                        nums = 0;
                        if (cells[i][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[i][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][j].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][j].getGb() == 1)
                            nums++;
                        if (nums >=3){
                            cells[i][j].setGb(1);
                            cells[i][j].setColor(true);
                            continue;
                        }

                        nums = 0;
                        if (cells[(i-1+size)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (cells[(i-1+size)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j+1)%size].getGb() == 1)
                            nums++;
                        if (cells[(i+1)%size][(j-1+size)%size].getGb() == 1)
                            nums++;
                        if (nums >=3){
                            cells[i][j].setGb(1);
                            cells[i][j].setColor(true);
                            continue;
                        }*/
                    }
                }
            }


            //判断晶界
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    if(cells[i][j].getState() != cells[i][(j+1)%size].getState() || cells[i][j].getState() != cells[(i+1)%size][j].getState()){
                        cells[i][j].setColor(false);
                    }else{
                        cells[i][j].setColor(true);
                    }
                }
            }

            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    if (cells[i][j].getColor()){
                        cells[i][j].setGb(1);
                    }else {
                        cells[i][j].setGb(0);
                    }
                }
            }
            if(cas%10 == 0){
                int[][] imageArray = new int[size][size];
                for (int i = 0; i < size; i++) {
                    for (int j = 0; j < size; j++) {
                        if(cells[i][j].getColor()){
                            imageArray[i][j] = -1;
                        }else {
                            imageArray[i][j] = -362145;
                        }

                    }
                }
                //图片输出路径
                ImageWithArray.writeImageFromArray("F:\\test\\P"+P+"cas"+(10000-cas)+"state"+state+".png", "PNG", imageArray);
            }
        }
    }
}
